package net.homelinux.ajay.net.ondisk.view.client;

import net.homelinux.ajay.net.ondisk.model.client.AppObject;
import net.homelinux.ajay.net.ondisk.model.client.Link;
import net.homelinux.ajay.net.ondisk.model.client.MetaFactory;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeListener;
import com.google.gwt.user.client.ui.Widget;

public class CollectionCommandsTreeItem extends AppTreeItem {


  private static CollectionCommandsTreeItem dragTarget;


  public static CollectionCommandsTreeItem makeDragTarget() {
    CollectionCommandsTreeItem itm = new CollectionCommandsTreeItem();
    return itm;
  }
  
  private TextBox urlbox;
  private Button addButton;
  private int oldlength;
  private int draggedLinksAction;
  private Panel dragPanel;

  

  public CollectionCommandsTreeItem(String html) {
    super(html,null);
  }
  
//
//  public CollectionCommandsTreeItem(Widget widget) {
//    super(widget);
//  }
  
  public CollectionCommandsTreeItem(){
      final Panel dragTarget = new FlowPanel();
      
      dragTarget.add(new Label("For the links dragged below:"));
      
      final ListBox choice = new ListBox();
      choice.addItem("Leave for editing","0");
      choice.addItem("Get size data","1");
      choice.addItem("Add to collection","2");
      choice.addChangeListener(new ChangeListener(){

        public void onChange(Widget sender) {
          setDraggedLinksAction(choice.getSelectedIndex());
          
        }});
      
      dragTarget.add(choice);

      dragTarget.add(new Label("Drag link or type URL:"));
      
      urlbox = new TextBox(){
        public void onBrowserEvent(Event event){
          onEditChange();
          super.onBrowserEvent(event);
          DOM.eventCancelBubble(event, true);
        }
      };
      
      urlbox.sinkEvents(Event.ONCHANGE|Event.MOUSEEVENTS|Event.FOCUSEVENTS);
      
      dragTarget.add(urlbox);
      
      
      addButton = new Button("Add to collection");
      addButton.addClickListener(new ClickListener(){

        public void onClick(Widget sender) {
          if(sender==addButton){
            addUrl2Collection();
          }
        }
        });
      dragTarget.add(addButton);
//      treepanel.add(dragTarget);
      
      this.dragPanel = dragTarget;
      
      setWidget(dragTarget);
      
      
      
      
      
      
    }

  private void addToCollection() {
    // TODO Auto-generated method stub
    System.out.println("adding the above to collection");
    
  }


  private void addUrl2Collection() {
    FolderTreeItem parent = (FolderTreeItem) getParentItem();
    if(parent!=null){
      parent.makeLinkChild(urlbox.getText());
    }
  }

  public AppTreeItem makeChild(String text) {
    throw new RuntimeException("NO IMPLEMENT");
  }

  public void onDeselect() {
    //setWidget(new Label("Add Links ..."));

  }

  private void onEditChange() {
    String text = urlbox.getText();
    if(text.length()>oldlength){
      oldlength = text.length()+1;
      //urlbox.setText("");
      //urlbox2.setText(text);
      if(draggedLinksAction>0)
        processUrl(text);
      if(draggedLinksAction>1)
        urlbox.setText("");
        addToCollection();
      
    }else{ 
      oldlength = text.length()+1;
    }
  }

  public void onSelect() {
//  TODO: this goes to the onSelect of the item.
//  DialogBox box = new DialogBox(true);
//  box.setWidget(dragPanel);
//  box.setPopupPosition(item.getAbsoluteLeft(), item.getAbsoluteTop());
//  box.setTitle(dragItem.getParentItem().getText());
//  box.show();
    //setWidget(dragPanel);

  }

  private void processLoadedMeta(AppObject meta) {
    // TODO Auto-generated method stub
    
  }
  
  private void processUrl(String url) {
    System.out.println(url);
    final Link meta = MetaFactory.getInstance().getMeta(url);
    Timer t = new Timer(){

      public void run() {
        if(meta.getHttpLoadStatus()>=Link.LOADED){
          this.cancel();
          processLoadedMeta(meta);
        }
      }
      
    };
    t.scheduleRepeating(1000);
  }

  private void setDraggedLinksAction(int ind) {
    draggedLinksAction = ind;

  }

  public static CollectionCommandsTreeItem getDragTarget() {
    if(dragTarget == null){
      dragTarget = makeDragTarget();
    }
    return dragTarget;
  }

  protected CommandsMenu getCommandsMenu() {
    // TODO Auto-generated method stub
    return null;
  }


}
